<?php

use yii\helpers\Url;
?>
<style>
<?php $this->beginBlock('CSS') ?>
    option.lost{
        text-decoration: line-through;
    }
<?php $this->endBlock() ?>
</style>
<script type="text/javascript">
<?php $this->beginBlock('JS_END') ?>
    yii.process = (function ($) {
        var pub = {
            action: function () {
                var action = $(this).data('action');
                var params = $((action == 'assign' ? '#all_roles' : '#exists_roles')+ ', #role_id').serialize();
                var urlAssign = '<?= Url::toRoute(['assign', 'action' => 'assign']) ?>';
                var urlDelete = '<?= Url::toRoute(['assign', 'action' => 'delete']) ?>';

                $.post(action == 'assign' ? urlAssign : urlDelete,
                    params, function (r) {
                        console.log(r[1]);
                        $('#all_roles').html(r[0]);
                        $('#exists_roles').html(r[1]);
                    }, 'json');

                return false;
            }
        }

        return pub;
    })(window.jQuery);
<?php $this->endBlock(); ?>

<?php $this->beginBlock('JS_READY') ?>
    $('a[data-action]').click(yii.process.action);
<?php $this->endBlock(); ?>
</script>
<?php
yii\web\YiiAsset::register($this);
$this->registerCss($this->blocks['CSS']);
$this->registerJs($this->blocks['JS_END'], yii\web\View::POS_END);
$this->registerJs($this->blocks['JS_READY'], yii\web\View::POS_READY);
